﻿using nhCore.db;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Org.BouncyCastle.Math.EC.ECCurve;

namespace envG
{
    public partial class Form曲线 : UIForm
    {
        private Station _station;
        private DataGridView _dgv;
        private Dictionary<int, int> _dicRegAddrColIndex;

        public Form曲线(Station station, DataGridView dgv, Dictionary<int, int> dicRegAddrColIndex)
        {
            InitializeComponent();

            _station = station;
            _dgv = dgv;
            _dicRegAddrColIndex = dicRegAddrColIndex;

            UiComboBox要素.Items.AddRange([.. _station.ElementConfigs]);
            UiComboBox要素.DisplayMember = nameof(ElementConfig.Name);
            if (UiComboBox要素.Items.Count > 0)
            {
                UiComboBox要素.SelectedIndex = 0;
            }
        }

        private void UiComboBox要素_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(sender is UIComboBox comboBox && comboBox.SelectedItem is ElementConfig config)) { return; }

            int index = _dicRegAddrColIndex[config.StationReg];

            UILineOption option = new();
            option.ToolTip.Visible = true;
            option.Title = new UITitle
            {
                Text = "曲线图",
                SubText = $"{config.Name}（{config.Unit}）",
            };

            option.XAxisType = UIAxisType.DateTime;

            var series = option.AddSeries(new UILineSeries(config.Name));

            series.Symbol = UILinePointSymbol.Square;
            series.SymbolSize = 4;
            series.SymbolLineWidth = 2;
            //series.SymbolColor = Color.Red;
            series.ShowLine = !UiCheckBix只显示点.Checked;
            //数据点显示小数位数
            series.YAxisDecimalPlaces = config.Digits;

            for (int i = _dgv.Rows.Count - 1; i > 0; i--)
            {
                DateTime time = (DateTime)_dgv.Rows[i].Cells[0].Value;
                if (_dgv.Rows[i].Cells[index].Value is double value)
                {
                    series.Add(time, value);

                }
            }

            series.Smooth = UiCheckBox平滑曲线.Checked;

            //option.GreaterWarningArea = new UILineWarningArea(3.5);
            //option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold);

            //option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red));
            //option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold));

            option.XAxis.Name = "日期";
            option.YAxis.Name = $"{config.Name}（{config.Unit}）";
            //X轴坐标轴显示格式化
            option.XAxis.AxisLabel.DateTimeFormat = "yyyy-MM-dd HH:mm";

            //Y轴坐标轴显示小数位数
            option.YAxis.AxisLabel.DecimalPlaces = config.Digits;

            //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red));
            //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red));

            //设置X轴显示范围
            //option.XAxis.SetRange(dt, dt.AddHours(8));
            UiLineChart要素.SetOption(option);
        }

        private void UiCheckBix只显示点_CheckedChanged(object sender, EventArgs e)
        {
            UiComboBox要素_SelectedIndexChanged(UiComboBox要素, new EventArgs());
        }

        private void UiSymbolButton导出_Click(object sender, EventArgs e)
        {
            if (UiComboBox要素.SelectedItem is not ElementConfig config) { return; }

            SaveFileDialog dlg = new()
            {
                Filter = "JPEG|*.jpg",
                FileName = $"{config.Name}曲线图"
            };
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                UiLineChart要素.BackgroundImage?.Save(dlg.FileName, ImageFormat.Jpeg);
            }
        }
    }
}
